﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BALayer;
using System.Data.SqlClient;

namespace PGBT
{
    public partial class FormDatHang : Form
    {
        private GiaoDich dbGG = null;
        private NhaCungCap dbNCC = null;
        private HangHoa dbHH = null;

        private DataSet dsNhaCungCap = null;
        private DataSet dsHangHoa = null;

        public FormDatHang()
        {
            InitializeComponent();

            dbGG = new GiaoDich();
            dbNCC = new NhaCungCap();
            dbHH = new HangHoa();
        }

        private void FormDatHang_Load(object sender, EventArgs e)
        {
            dsNhaCungCap = dbNCC.DanhSachNhaCungCap();
            dsHangHoa = dbHH.DanhSachHangHoa();

            cbTenNcc.DataSource = dsNhaCungCap.Tables[0];
            cbTenNcc.DisplayMember = "tenncc";

            cbTenHang.DataSource = dsHangHoa.Tables[0];
            cbTenHang.DisplayMember = "tenhang";

            dsHangHoa = dbHH.DanhSachHangHoa();
            dsNhaCungCap = dbNCC.DanhSachNhaCungCap();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string error = "";
            
            bool done = false;

            dbGG.BeginTransaction();

            try
            {
                int madonhang = dbGG.TaoDonHang(ref error, Convert.ToInt32(txtIdNcc.Text), DateTime.Now);

                if (madonhang == -1) throw new Exception("Không thể tạo đơn hàng !");

                for (int i = 0; i < dgvHangHoa.Rows.Count - 1; i++)
                {
                    int mahang = Convert.ToInt32(dgvHangHoa.Rows[i].Cells[0].Value.ToString());
                    int soluong = Convert.ToInt32(dgvHangHoa.Rows[i].Cells[2].Value.ToString());
                    if (!dbGG.ThemVaoDonHang(ref error, madonhang, mahang, soluong)) throw new Exception("Không thể thêm hàng vào đơn hàng !");
                }

                dbGG.CommitTransaction();
                done = true;

            }
            catch (Exception ex)
            {
                dbGG.RollbackTransaction();
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if(done)
            {
                MessageBox.Show("Đặt hàng thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int index = cbTenHang.SelectedIndex;

            int mahang = Convert.ToInt32(dsHangHoa.Tables[0].Rows[index].ItemArray[0].ToString());
            String tenhang = dsHangHoa.Tables[0].Rows[index].ItemArray[1].ToString();
            int soluong = 0;

            if(txtSoLuong.Text != "")
            {
                soluong = Convert.ToInt32(txtSoLuong.Text);
            }

            if(soluong > 0)
            {
                dgvHangHoa.Rows.Add(mahang, tenhang, soluong);
            }
            else
            {
                MessageBox.Show("Vui lòng nhập số lượng muốn đặt !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void cbTenNcc_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtIdNcc.Text = dsNhaCungCap.Tables[0].Rows[cbTenNcc.SelectedIndex].ItemArray[0].ToString();
        }

        private void cbTenHang_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}
